By Dimitre Novatchev |
Language | XPATH |
Category | designpatterns, xml, msxml |
Posted | 05 Apr 2001 |
Updated | 05 Apr 2001 |
Summary |
This snippet shows how to implement the abs() function as a single XPath expression |
We need an XPath implementation of the classical abs() math function, defined as: |
abs(x) = x iff x >= 0 |
abs(x) = -x otherwise |
The expression is: |
concat(substring('-', 2 - ($pNum <0)), '1') * $pNum |
For a more general approach to conditionally generating strings within a single XPath expression, see another snippet: |
http://www.vbxml.com/snippetcentral/main.asp?view=viewsnippet&id=v20010328225824 |
Other XPath expressions implementing the abs() function have been also suggested by Bejamin Guralnik, John Ardiss, and Samuel Chen: |
$pNum * (translate(number(boolean(0 >= $pNum )),'10','13') - 2) |
((($pNum > 0) * $pNum) + (($pNum < 0) * -$pNum)) |
substring('-1', 2 - ($pNum < 0)) * $pNum |